﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NcfDB.parsing
{
    class parsing
    {
        /// <summary>
        /// 分割字符串，输入字符串，输出字符串数组
        /// </summary>
        /// <param name="source">源数据</param>
        /// <returns>list string </returns>
        public static List<string> tostringlist(string source)
        {

            List<string> target = new List<string>();
            int insertlocation = 0;
            while (source.IndexOf("*", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("*", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = source.IndexOf("*", insertlocation) + 3;
            }
            insertlocation = 0;
            while (source.IndexOf(",", insertlocation) != -1)
            {
                insertlocation = source.IndexOf(",", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = source.IndexOf(",", insertlocation) + 3;
            }
            insertlocation = 0;
            while (source.IndexOf("-", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("-", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = insertlocation + 3;
            }
            insertlocation = 0;
            while (source.IndexOf("/", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("/", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = insertlocation + 3;

            }
            insertlocation = 0;
            while (source.IndexOf("+", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("+", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = insertlocation + 3;

            }
            insertlocation = 0;
            while (source.IndexOf("(", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("(", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = insertlocation + 3;
            }
            insertlocation = 0;
            while (source.IndexOf(")", insertlocation) != -1)
            {
                insertlocation = source.IndexOf(")", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 2, " ");
                insertlocation = insertlocation + 3;
            }
            insertlocation = 1;
            while (source.IndexOf("=", insertlocation) != -1)
            {
                string str1;
                str1 = source.Substring(source.IndexOf("=", insertlocation) - 1, 2);
                if (str1 != ">=" && str1 != "<=")
                {
                    insertlocation = source.IndexOf("=", insertlocation);
                    source = source.Insert(insertlocation, " ");
                    source = source.Insert(insertlocation + 2, " ");
                }
                insertlocation = source.IndexOf("=", insertlocation) + 2;

            }
            insertlocation = 0;
            while (source.IndexOf(">=", insertlocation) != -1)
            {
                insertlocation = source.IndexOf(">=", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 3, " ");
                insertlocation = source.IndexOf(">=", insertlocation) + 3;

            }
            insertlocation = 0;
            while (source.IndexOf("<=", insertlocation) != -1)
            {
                insertlocation = source.IndexOf("<=", insertlocation);
                source = source.Insert(insertlocation, " ");
                source = source.Insert(insertlocation + 3, " ");
                insertlocation = source.IndexOf("<=", insertlocation) + 3;

            }
            insertlocation = 0;
            while (source.IndexOf(">", insertlocation) != -1)
            {
                string str1;
                str1 = source.Substring(source.IndexOf(">", insertlocation), 2);
                if (str1 != ">=" && str1 != "<=")
                {
                    insertlocation = source.IndexOf(">", insertlocation);
                    source = source.Insert(insertlocation, " ");
                    source = source.Insert(insertlocation + 2, " ");
                }
                insertlocation = source.IndexOf(">", insertlocation) + 2;

            }
            insertlocation = 0;
            while (source.IndexOf("<", insertlocation) != -1)
            {
                string str1;
                str1 = source.Substring(source.IndexOf("<", insertlocation), 2);
                if (str1 != ">=" && str1 != "<=")
                {
                    insertlocation = source.IndexOf("<", insertlocation);
                    source = source.Insert(insertlocation, " ");
                    source = source.Insert(insertlocation + 2, " ");
                }
                insertlocation = source.IndexOf("<", insertlocation) + 2;

            }
            string[] s = source.Split(new char[] { ' ','\n','\r'});
            s = s.Where(a => !string.IsNullOrEmpty(a)).ToArray();
            List<string> d = new List<string>(s);
            target = d;
            return target;
        }
        /// <summary>
        /// 输入字符串数组，返回Word列表
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<Word> types(List<string> source)
        {
            List<Word> target = new List<Word>();

            for (int i = 0; i < source.Count; i++)
            {
                Word ex = new Word();
                target.Add(ex);
                target[i].type = WORD_TYPE_ENUM.INVALID_WORD;
            }
            for (int i = 0; i < source.Count; i++)
            {
                double num;
                if (source[i].ToUpper() == "SELECT")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.SELECT;
                }
                else if (source[i].ToUpper() == "DELETE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.DELETE;
                }
                else if (source[i].ToUpper() == "UPDATE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.UPDATE;
                }
                else if (source[i].ToUpper() == "INSERT")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.INSERT;
                }
                else if (source[i].ToUpper() == "DROP")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.DROP;
                }
                else if (source[i].ToUpper() == "FROM")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FROM;
                }
                else if (source[i].ToUpper() == "WHERE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.WHERE;
                }
                else if (source[i].ToUpper() == "CREATE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.CREATE;
                }
                else if (source[i].ToUpper() == "TABLE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.TABLE;
                }
                else if (source[i].ToUpper() == "VALUES")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.VALUES;
                }
                else if (source[i].ToUpper() == "DATABASE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.DATABASE;
                }
                else if (source[i].ToUpper() == "INTO")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.INTO;
                }
                else if (source[i].ToUpper() == "AND")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.AND;
                }
                else if (source[i].ToUpper() == "LIKE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.LIKE;
                }
                else if (source[i].ToUpper() == "OR")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.OR;
                }
                else if (source[i].ToUpper() == "SET")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.SET;
                }
                else if (source[i] == "+")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.PLUS;
                }
                else if (source[i] == "-")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.MINUS;
                }
                else if (source[i] == ",")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.COMMA;
                }
                else if (source[i].ToUpper() == "*")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.STAR;
                }
                else if (source[i].ToUpper() == "/")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.DELETE;
                }
                else if (source[i].ToUpper() == ">=")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.GEQ;
                }
                else if (source[i].ToUpper() == "<=")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.LEQ;
                }
                else if (source[i].ToUpper() == "=")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.EQL;
                }
                else if (source[i].ToUpper() == "<")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.LES;
                }
                else if (source[i].ToUpper() == ">")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.GTR;
                }
                else if (source[i].ToUpper() == "(")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.LEFT_PARENTHESIS;
                }
                else if (source[i].ToUpper() == ")")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.RIGHT_PARENTHESIS;
                }
                else if (source[i].ToUpper() == "CHAR")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FIELD_TYPE;
                }
                else if (source[i].ToUpper() == "INT")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FIELD_TYPE;
                }
                else if (source[i].ToUpper() == "DOUBLE")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FIELD_TYPE;
                }
                else if (source[i].ToUpper() == "BOOL")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FIELD_TYPE;
                }
                else if (source[i] == "true")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.BOOL;
                    target[i].value = source[i];
                }
                else if (source[i] == "false")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.BOOL;
                    target[i].value = source[i];
                }
                else if (source[i] == "KEY")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.KEY_FLAG;
                }
                else if (source[i].ToUpper() == "EDIT")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.EDIT;
                }
                else if (source[i].ToUpper() == "RENAME")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.RENAME;
                }
                else if (source[i].ToUpper() == "BETWEEN")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.BETWEEN;
                }
                else if (source[i] == "NOT_KEY")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.KEY_FLAG;
                }
                else if (source[i] == "NO_NULL")
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.NULL_FLAG;
                }
                else if (double.TryParse(source[i], out num))
                {
                    target[i].name = source[i].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.VALUE;
                    target[i].value = source[i].ToUpper();
                }
                else if (source[i].IndexOf("'") > -1)
                {
                    string[] s = source[i].Split(new char[] { '\'' });
                    target[i].name = source[i];
                    target[i].type = WORD_TYPE_ENUM.VALUE;
                    target[i].value = s[1];
                }

                else if (source[i].IndexOf("[") > -1)
                {
                    string[] s = source[i].Split(new char[] { '[', ']' });
                    target[i].name = s[0].ToUpper();
                    target[i].type = WORD_TYPE_ENUM.FIELD_TYPE;
                    target[i].value = s[1];
                }
                else
                {
                    target[i].name = source[i];
                    target[i].type = WORD_TYPE_ENUM.IDENTIFIER;
                }
            }
            Word last=new Word();
            target.Add(last);
            return target;
        }
        /// <summary>
        /// <summary>
        /// 输入字符串，返回Word列表
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static List<Word> parse(string source)
        {
            List<string> mid = new List<string>();
            List<Word> target = new List<Word>();
            mid = parsing.tostringlist(source);
            target = parsing.types(mid);
            return target;
        }

    }
}
